package com.cms.action;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.action.DefaultAction;
import com.cms.manager.ChannelPercentManager;
import com.cms.model.AppInfo;
import com.cms.model.AppVersionInfo;
import com.cms.model.Channel;
import com.cms.model.ChannelPercent;
import com.cms.model.ImportUserChannelData;
import com.cms.model.ImportUserData;
import com.cms.service.AppInfoServiceImpl;
import com.cms.service.AppVersionInfoServiceImpl;
import com.cms.service.ChannelServiceImpl;
import com.cms.service.ImportUserDataService;
import com.cms.service.ImportUserDataServiceImpl;
import com.util.BaseEntity;
import com.util.Util;

public class ChannelPrercentAction extends DefaultAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6262889931867879263L;
	
	private ChannelPercentManager manager = ChannelPercentManager.getInstance();
	private Map<Integer,List<ChannelPercent>> chpmap;
	private String msg="";
	private ChannelPercent model=new ChannelPercent();
	private List<Channel> chlist=new ChannelServiceImpl().getChannels();
	private List<AppInfo> applist = new AppInfoServiceImpl().getAppInfos();
	private List<AppVersionInfo> apvlist;
	
	List<ImportUserChannelData> chuserlist = new ArrayList<ImportUserChannelData>();
	Map<String,String> parms = new HashMap<String, String>();
	
	public String list(){
		chpmap = manager.getChpMap();
		apvlist = new AppVersionInfoServiceImpl().getAppVersionInfos();
		return SUCCESS;
	}

	public String show()
	{
		if(msg.equals("update"))
		{
			model = manager.getChannelPercent(model.getId());
			apvlist = new AppVersionInfoServiceImpl().getAppVersionInfoByAppNumber(model.getAppnumber());
		}

		return SUCCESS;
	}
	
	public String save()
	{
		if(msg.equals("insert"))
		{
			model.setCreatetime(Util.dateToString(new Date()));
			model.setCreateuser(currentAdmin.getLoginName());
			if(manager.saveChannelPercent("insert", model))
			{
				msg="CPA数据配置：添加配置成功！！";
			}
			else
			{
				msg="CPA数据配置：对不起，添加配置可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("update"))
		{
			if(manager.saveChannelPercent("update", model))
			{
				msg="CPA数据配置：更新配置成功！！";
			}
			else
			{
				msg="CPA数据配置：对不起，更新配置可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("del"))
		{
			if(manager.saveChannelPercent("delete", model))
			{
				msg="CPA数据配置：删除配置成功！！";
			}
			else
			{
				msg="CPA数据配置：对不起，删除配置可能存在问题，请稍候重试一下！！";
			}
		}else if(msg.equals("saveusernum")){
			ImportUserDataService service = new ImportUserDataServiceImpl();
			List<ImportUserData> l = new ArrayList<ImportUserData>();
			int i =(int)(Math.random()*9000+1000);
			for (ImportUserChannelData item : chuserlist) {
				item.setDatafileid(i);
				ImportUserData d = new ImportUserData();
				d.setDatafileid(item.getDatafileid());
				d.setDay(item.getDay());
				d.setAppnumber(item.getAppnumber());
				d.setAppversionnumber(item.getAppversionnumber());
				d.setOperator(item.getOperator());
				d.setChannelid(item.getChannelid());
				d.setChid(item.getChid());
				d.setSum(item.getSum1());
				d.setFee(item.getFee());
				l.add(d);
			}
			if(service.saveImportUserData(l)){
				msg="CPA用户数：确认导入成功！！";
				service.saveImportUserChannelData(chuserlist);
			}else{
				msg="CPA用户数：确认导入失败！！请稍后重试！！";
			}
		}
		return SUCCESS;
	}
	
	public String index(){
		if(!msg.equals("query")){
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DATE, -1);
			parms.put("startdate", Util.dateToString(c.getTime(), "yyyyMMdd"));
			parms.put("enddate", Util.dateToString(c.getTime(), "yyyyMMdd"));
		}else{
			int day1 = Integer.parseInt(parms.get("startdate"));
			int day2 = Integer.parseInt(parms.get("enddate"));
			init(day1,day2);
		}
		return SUCCESS;
	}
	 public void init(int day1,int day2){
		 ImportUserDataService service = new ImportUserDataServiceImpl();
		 List<ImportUserData> usernumlist = service.getImportUserDataUserNumByDay(day1,day2);
		 List<ImportUserData> feelist = service.getImportUserDataFeeByDay(day1,day2);
		 apvlist = new AppVersionInfoServiceImpl().getAppVersionInfos();
		 //匹配Cpa的用户数 收入
		 List<ImportUserData> list = new ArrayList<ImportUserData>();
		 for (ImportUserData usernum_item : usernumlist) {
			for (ImportUserData fee_item : feelist) {
				if(usernum_item.getDay()==fee_item.getDay() && 
						usernum_item.getAppversionnumber().equals(fee_item.getAppversionnumber()) &&
						usernum_item.getChannelid()==fee_item.getChannelid() &&
						usernum_item.getOperator().equals(fee_item.getOperator())){
					usernum_item.setFee(fee_item.getFee());
					break;
				}
			}
			//set appnumber
			for (AppVersionInfo appv : apvlist) {
				if(appv.getAppversionnumber().equals(usernum_item.getAppversionnumber())){
					usernum_item.setAppnumber(appv.getAppnumber());
					break;
				}
			}
			if(usernum_item.getFee()>0||usernum_item.getSum()>0){				
				list.add(usernum_item);
			}
		}
		 
		 System.out.println("================UserNumAndFee=================");
		 for (ImportUserData importUserData : list) {
			System.out.println(BaseEntity.toString(importUserData));
		}
		{
			//匹配 渠道 扣量 用户数
			List<ChannelPercent> chplist = manager.getChannelPercents();
			for (ImportUserData item : list) {
				double usersum=item.getSum();
				for (ChannelPercent channelPercent : chplist) {
					if(item.getChannelid()==channelPercent.getChannelid() &&
							item.getAppversionnumber().equals(channelPercent.getAppversionnumber())){
						
						item.setSum1(item.getSum());
						usersum = usersum * (channelPercent.getDaypercent() / 100d);
						item.setSum((int)usersum);
						chuserlist.add(new ImportUserChannelData(item));
					}
				}
			}
			System.out.println("================UserNum=================");
			 for (ImportUserChannelData it : chuserlist) {
				System.out.println(BaseEntity.toString(it));
			 }
		 }
	 }
	 
	 public String computearpu(float fee,int activeuser)
	{
		String arpu = "--";
		if(activeuser >0 && fee>0)
		{
			float f = fee/activeuser;
			DecimalFormat format = new DecimalFormat("#.00");
	        arpu = format.format(f);
		}
		return arpu;
	}
	 
	public String changeappversion(){
		apvlist = new AppVersionInfoServiceImpl().getAppVersionInfoByAppNumber(model.getAppnumber());
		return SUCCESS;
	}
	public Map<Integer, List<ChannelPercent>> getChpmap() {
		return chpmap;
	}

	public void setChpmap(Map<Integer, List<ChannelPercent>> chpmap) {
		this.chpmap = chpmap;
	}

	public ChannelPercent getModel() {
		return model;
	}

	public void setModel(ChannelPercent model) {
		this.model = model;
	}

	public List<Channel> getChlist() {
		return chlist;
	}

	public void setChlist(List<Channel> chlist) {
		this.chlist = chlist;
	}

	public List<AppInfo> getApplist() {
		return applist;
	}

	public void setApplist(List<AppInfo> applist) {
		this.applist = applist;
	}

	public List<AppVersionInfo> getApvlist() {
		return apvlist;
	}

	public void setApvlist(List<AppVersionInfo> apvlist) {
		this.apvlist = apvlist;
	}


	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<ImportUserChannelData> getChuserlist() {
		return chuserlist;
	}

	public void setChuserlist(List<ImportUserChannelData> chuserlist) {
		this.chuserlist = chuserlist;
	}

	public Map<String, String> getParms() {
		return parms;
	}

	public void setParms(Map<String, String> parms) {
		this.parms = parms;
	}

}
